package com.web.controller;

import com.aop.SystemLog;
import com.dto.BaseDto;
import com.entity.Condition;
import com.entity.Employrecord;
import com.page.PageInfo;
import com.web.service.EmployrecordService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/employrecordController")
public class EmployrecordController {
    @Resource
    EmployrecordService employrecordService;

    @RequestMapping("/employrecords")
    @ResponseBody
   public BaseDto<Employrecord> getAll() throws Exception{
//        System.out.println("getAll");
//       List<Student> students = Arrays.asList(new Student[]{new Student("0901", "张三"), new Student("0902", "李四"), new Student("0901", "王二")});
//       return students;
        return employrecordService.getAll();
   }


    @SystemLog(module = "合同管理",methods = "分页查询")
    @RequestMapping("/employrecords/page/json")
    @ResponseBody
    public BaseDto<Employrecord> getEmployrecordsByPageJson(PageInfo pageInfo) throws Exception{

        return employrecordService.getEmployrecordsByPage(pageInfo);
    }


    @RequestMapping("/employrecords/page")
    public ModelAndView getEmployrecordsByPage(PageInfo pageInfo) throws Exception{
        BaseDto<Employrecord> dto = employrecordService.getEmployrecordsByPage(pageInfo);

        return new ModelAndView("/back/employrecord/list", "dto", dto);
    }

    //@RequestMapping(name = "/employrecord",method = RequestMethod.POST)
    @PostMapping("/employrecord")
    @ResponseBody
    public BaseDto<Employrecord> add(Employrecord employrecord) throws Exception{
        return employrecordService.add(employrecord);
    }


    @PostMapping("/dietitians/search/{currPage}")
    @ResponseBody
    public BaseDto  searchEmployeesByPage(@PathVariable("currPage") String currPage,@RequestBody List<Condition> conditions) throws Exception{
        PageInfo pageInfo = new PageInfo();
        int realCurrPage = Integer
                .parseInt(Optional.ofNullable(currPage).orElse("1"));//如果为kong 默认为1 如果非法字符抛出集中处理
        pageInfo.setCurrPage(realCurrPage);
        BaseDto dto = employrecordService.searchEmployeesByPage(pageInfo,conditions);
        //下一面一句等价于 request.getDispatcherRequest.forward + request.setAttibute("dto",dto)
        dto.setObj(pageInfo);//dto带上分页信息
        return dto;
    }


}
